/**
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.lookahead.parent.common.utils;


import org.lookahead.parent.apidoc.enums.NodeTeachingResourceEnum;
import org.lookahead.parent.apidoc.enums.QTypeEnum;
import org.lookahead.parent.apidoc.enums.QuestionCategoryEnum;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 通用工具类
 *
 * @author LookAhead
 */
public class CommonUtil {
    /**
     * 所有题型
     */
    public static Map<Integer, String> questionTypeMap = new HashMap<>();
    /**
     * 连线题 题型分类
     */
    public static Map<Integer, String> questionCategoryMap = new HashMap<>();
    /**
     * 单选题型
     */
    public static Map<Integer, String> sigMap = new HashMap<>();
    /**
     * 多选题并且拼接有序
     */
    public static Map<Integer, String> mulMap = new HashMap<>();
    /**
     * 多选题并且拼接有序
     */
    public static Map<Integer, String> sortMap = new HashMap<>();
    /**
     * 分组多选题
     */
    public static Map<Integer, String> arrMap = new HashMap<>();
    /**
     * 完形填空
     */
    public static Map<Integer, String> clozeMap = new HashMap<>();
    /**
     * node_teaching_content不存在的资源类型
     */
    public static Map<Integer, String> notResourcesContentMap = new HashMap<>();
    /**
     * node_teaching_content存在的资源类型
     */
    public static Map<Integer, String> resourcesContentMap = new HashMap<>();

    static {
        sigMap.put(QTypeEnum.CHOICE_1.getValue(), QTypeEnum.CHOICE_1.getName());
        sigMap.put(QTypeEnum.CHOICE_2.getValue(), QTypeEnum.CHOICE_2.getName());
        sigMap.put(QTypeEnum.CHOICE_3.getValue(), QTypeEnum.CHOICE_3.getName());
        sigMap.put(QTypeEnum.CHOICE_4.getValue(), QTypeEnum.CHOICE_4.getName());
        sigMap.put(QTypeEnum.CHOICE_5.getValue(), QTypeEnum.CHOICE_5.getName());
        sigMap.put(QTypeEnum.CHOICE_6.getValue(), QTypeEnum.CHOICE_6.getName());

        mulMap.put(QTypeEnum.COMPLETION_1.getValue(), QTypeEnum.COMPLETION_1.getName());
        mulMap.put(QTypeEnum.COMPLETION_2.getValue(), QTypeEnum.COMPLETION_2.getName());
        mulMap.put(QTypeEnum.COMPLETION_4.getValue(), QTypeEnum.COMPLETION_4.getName());

        sortMap.put(QTypeEnum.SORT_1.getValue(), QTypeEnum.SORT_1.getName());
        sortMap.put(QTypeEnum.SORT_2.getValue(), QTypeEnum.SORT_2.getName());

        arrMap.put(QTypeEnum.GROUP_1.getValue(), QTypeEnum.GROUP_1.getName());
        arrMap.put(QTypeEnum.MATCHING_1.getValue(), QTypeEnum.MATCHING_1.getName());
        arrMap.put(QTypeEnum.MATCHING_2.getValue(), QTypeEnum.MATCHING_2.getName());
        arrMap.put(QTypeEnum.MATCHING_3.getValue(), QTypeEnum.MATCHING_3.getName());
        arrMap.put(QTypeEnum.MATCHING_4.getValue(), QTypeEnum.MATCHING_4.getName());

        questionTypeMap.put(QTypeEnum.DUBBING_1.getValue(), QTypeEnum.DUBBING_1.getName());
        questionTypeMap.put(QTypeEnum.DUBBING_2.getValue(), QTypeEnum.DUBBING_2.getName());
        questionTypeMap.put(QTypeEnum.CHOICE_1.getValue(), QTypeEnum.CHOICE_1.getName());
        questionTypeMap.put(QTypeEnum.COMPLETION_1.getValue(), QTypeEnum.COMPLETION_1.getName());
        questionTypeMap.put(QTypeEnum.CHOICE_2.getValue(), QTypeEnum.CHOICE_2.getName());
        questionTypeMap.put(QTypeEnum.AUDIO_1.getValue(), QTypeEnum.AUDIO_1.getName());
        questionTypeMap.put(QTypeEnum.CHOICE_3.getValue(), QTypeEnum.CHOICE_3.getName());
        questionTypeMap.put(QTypeEnum.CHOICE_4.getValue(), QTypeEnum.CHOICE_4.getName());
        questionTypeMap.put(QTypeEnum.CHOICE_5.getValue(), QTypeEnum.CHOICE_5.getName());
        questionTypeMap.put(QTypeEnum.GROUP_1.getValue(), QTypeEnum.GROUP_1.getName());
        questionTypeMap.put(QTypeEnum.MATCHING_1.getValue(), QTypeEnum.MATCHING_1.getName());
        questionTypeMap.put(QTypeEnum.MATCHING_2.getValue(), QTypeEnum.MATCHING_2.getName());
        questionTypeMap.put(QTypeEnum.MATCHING_3.getValue(), QTypeEnum.MATCHING_3.getName());
        questionTypeMap.put(QTypeEnum.AUDIO_2.getValue(), QTypeEnum.AUDIO_2.getName());
        questionTypeMap.put(QTypeEnum.DUBBING_3.getValue(), QTypeEnum.DUBBING_3.getName());
        questionTypeMap.put(QTypeEnum.AUDIO_3.getValue(), QTypeEnum.AUDIO_3.getName());
        questionTypeMap.put(QTypeEnum.AUDIO_4.getValue(), QTypeEnum.AUDIO_4.getName());
        questionTypeMap.put(QTypeEnum.COMPLETION_2.getValue(), QTypeEnum.COMPLETION_2.getName());
        questionTypeMap.put(QTypeEnum.SORT_1.getValue(), QTypeEnum.SORT_1.getName());
        questionTypeMap.put(QTypeEnum.CHOICE_6.getValue(), QTypeEnum.CHOICE_6.getName());
        questionTypeMap.put(QTypeEnum.AUDIO_5.getValue(), QTypeEnum.AUDIO_5.getName());
        questionTypeMap.put(QTypeEnum.MATCHING_4.getValue(), QTypeEnum.MATCHING_4.getName());
        questionTypeMap.put(QTypeEnum.SORT_2.getValue(), QTypeEnum.SORT_2.getName());
        questionTypeMap.put(QTypeEnum.DUBBING_4.getValue(), QTypeEnum.DUBBING_4.getName());
        questionTypeMap.put(QTypeEnum.DUBBING_5.getValue(), QTypeEnum.DUBBING_5.getName());
        questionTypeMap.put(QTypeEnum.CHOICE_7.getValue(), QTypeEnum.CHOICE_7.getName());
        questionTypeMap.put(QTypeEnum.READ_2.getValue(), QTypeEnum.READ_2.getName());
        questionTypeMap.put(QTypeEnum.READ_1.getValue(), QTypeEnum.READ_1.getName());
        questionTypeMap.put(QTypeEnum.SPELL_1.getValue(), QTypeEnum.SPELL_1.getName());
        questionTypeMap.put(QTypeEnum.SPELL_2.getValue(), QTypeEnum.SPELL_2.getName());
        questionTypeMap.put(QTypeEnum.COMPLETION_4.getValue(), QTypeEnum.COMPLETION_4.getName());

        questionCategoryMap.put(QuestionCategoryEnum.DUBBING.getKey(), QuestionCategoryEnum.DUBBING.getValue());
        questionCategoryMap.put(QuestionCategoryEnum.CHOICE.getKey(), QuestionCategoryEnum.CHOICE.getValue());
        questionCategoryMap.put(QuestionCategoryEnum.AUDIO.getKey(), QuestionCategoryEnum.AUDIO.getValue());
        questionCategoryMap.put(QuestionCategoryEnum.MATCHING.getKey(), QuestionCategoryEnum.MATCHING.getValue());
        questionCategoryMap.put(QuestionCategoryEnum.COMPLETION.getKey(), QuestionCategoryEnum.COMPLETION.getValue());
        questionCategoryMap.put(QuestionCategoryEnum.GROUP.getKey(), QuestionCategoryEnum.GROUP.getValue());
        questionCategoryMap.put(QuestionCategoryEnum.SORT.getKey(), QuestionCategoryEnum.SORT.getValue());
        questionCategoryMap.put(QuestionCategoryEnum.SPELL.getKey(), QuestionCategoryEnum.SPELL.getValue());
        questionCategoryMap.put(QuestionCategoryEnum.READ.getKey(), QuestionCategoryEnum.READ.getValue());

        notResourcesContentMap.put(NodeTeachingResourceEnum.RESOURCE_TYPE_1.getKey(), NodeTeachingResourceEnum.RESOURCE_TYPE_1.getValue());
        notResourcesContentMap.put(NodeTeachingResourceEnum.RESOURCE_TYPE_2.getKey(), NodeTeachingResourceEnum.RESOURCE_TYPE_2.getValue());
        notResourcesContentMap.put(NodeTeachingResourceEnum.RESOURCE_TYPE_3.getKey(), NodeTeachingResourceEnum.RESOURCE_TYPE_3.getValue());
        notResourcesContentMap.put(NodeTeachingResourceEnum.RESOURCE_TYPE_4.getKey(), NodeTeachingResourceEnum.RESOURCE_TYPE_4.getValue());
        notResourcesContentMap.put(NodeTeachingResourceEnum.RESOURCE_TYPE_8.getKey(), NodeTeachingResourceEnum.RESOURCE_TYPE_8.getValue());
        notResourcesContentMap.put(NodeTeachingResourceEnum.RESOURCE_TYPE_10.getKey(), NodeTeachingResourceEnum.RESOURCE_TYPE_10.getValue());

        resourcesContentMap.put(NodeTeachingResourceEnum.RESOURCE_TYPE_11.getKey(), NodeTeachingResourceEnum.RESOURCE_TYPE_11.getValue());
        resourcesContentMap.put(NodeTeachingResourceEnum.RESOURCE_TYPE_5.getKey(), NodeTeachingResourceEnum.RESOURCE_TYPE_5.getValue());
        resourcesContentMap.put(NodeTeachingResourceEnum.RESOURCE_TYPE_6.getKey(), NodeTeachingResourceEnum.RESOURCE_TYPE_6.getValue());
        resourcesContentMap.put(NodeTeachingResourceEnum.RESOURCE_TYPE_7.getKey(), NodeTeachingResourceEnum.RESOURCE_TYPE_7.getValue());
        resourcesContentMap.put(NodeTeachingResourceEnum.RESOURCE_TYPE_9.getKey(), NodeTeachingResourceEnum.RESOURCE_TYPE_9.getValue());
    }

    public static Integer getQuestionTypeByName(String name) {
        for (Integer key : questionTypeMap.keySet()) {
            String value = questionTypeMap.get(key);
            if (value.equals(name)) {
                return key;
            }
        }
        return null;
    }

    public static String getRandom(Integer n) {
        StringBuilder result = new StringBuilder();
        if (null != n && n > 0) {
            Random random = new Random();
            for (int i = 0; i < n; i++) {
                result.append(random.nextInt(10));
            }
        }
        return result.toString();
    }

    public static String removeLastComma(String str) {
        if (null == str || "".equals(str)) {
            return str;
        }
        return str.contains(",") ? str.substring(0, str.length() - 1) : str;
    }

    public static String generateSequence(int n) {
        char[] letters = {
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
                'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
                'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        boolean[] flags = new boolean[letters.length];
        char[] chs = new char[n];
        for (int i = 0; i < chs.length; i++) {
            int index;
            do {
                index = (int) (Math.random() * (letters.length));
            } while (flags[index]);// 判断生成的字符是否重复
            chs[i] = letters[index];
            flags[index] = true;
        }
        return new String(chs);
    }

    public static String parseDouble(Double d) {
        DecimalFormat df = new DecimalFormat("0.0");
        return df.format(d);
    }

    /**
     * 计算剩余天数
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @return 剩余天数
     */
    public static Long getRemainDays(Date startDate, Date endDate) {
        if (null != startDate && null != endDate) {
            long days = (endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000);
            return days > 0 ? days : 0;
        }
        return 0L;
    }

    public static void sleep(long n) {
        try {
            Thread.sleep(n);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
